$(function () {
    var num = 1;
    $.ajax({
        url: "/water_saving/select",
        type: "get",
        async: false,
        data: 'evaBasis=' + label_eva_basis,
        success: function (result) {
            var ele = document.getElementById("textArea");
            var list = result['rows'];
            for (var i = 0; i < list.length; i++) {
                ele.value = ele.value + num + ". " + list[i].requirement + "\n";
                num++;
            }
        },
        error: function () {
            bootoast({
                message: '通讯异常',
                position: 'top',
                type: 'danger',
                timeout: 1.5,
            });
        }
    });

});

$('#satisfy').click(function () {
    //从META_MANAGEMENT_INDICATOR中读取数据，插入TABLE_WATER_SAVING
    getManagementIndicatorData();
    getTechnicalIndicatorData();

    $('#page-wrapper').load('pages/table_water_saving.html');
})

$('#dissatisfy').click(function () {
    $('#page-wrapper').load('pages/label_water_saving.html');
})

function terms() {
    if (!$("input[type='checkbox']").is(':checked')) {
        $("#satisfy").attr("disabled", true).css("background-color", "#666");
    } else {
        $("#satisfy").attr("disabled", false).css("background", "#1E90FF");
    }
}

function getManagementIndicatorData() {
    $.ajax({
        url: "/water_saving/get_management_indicator_data",
        type: "get",
        async: false,
        data: 'standardNum=' + label_eva_basis,
        success: function (result) {
            list = result['rows'];
            insertManagementIndicatorData(list);
        },
        error: function () {
            bootoast({
                message: '通讯异常',
                position: 'top',
                type: 'danger',
                timeout: 1.5,
            });
        }
    });
}

function insertManagementIndicatorData(list) {

    //查询MANAGEMENT_INDICATOR中的已有数据，插入时避免重复
    var assessmentContentList = [];

    $.ajax({
        url: "/table_water_saving/management_select",
        data: 'last_id=' + label_id,
        type: "get",
        async: false,
        success: function (result) {
            var temp = result['rows'];
            for (var i = 0; i < temp.length; i++) {
                assessmentContentList.push(temp[i].assessmentContent);
            }
        },
        error: function () {
            bootoast({
                message: '通讯异常',
                position: 'top',
                type: 'danger',
                timeout: 1.5,
            });
        }
    });
    var assessmentIndicator;
    var assessmentContent;
    var assessmentMethod;
    var fullMark;
    var labelId;
    for (var i = 0; i < list.length; i++) {
        assessmentIndicator = list[i].assessmentIndicator;
        assessmentContent = list[i].assessmentContent;
        assessmentMethod = list[i].assessmentMethod;
        fullMark = list[i].fullMark;
        labelId = label_id;
        var insertFlag = true;
        for (var j = 0; j < assessmentContentList.length; j++) {
            if (assessmentContent == assessmentContentList[j]) {
                insertFlag = false;
                break;
            }
        }
        if (insertFlag) {
            $.ajax({
                type: "post",
                url: "/water_saving/insert_management_indicator_data",
                async: false,
                data: {
                    'assessmentIndicator': assessmentIndicator,
                    'assessmentContent': assessmentContent,
                    'assessmentMethod': assessmentMethod,
                    'fullMark': fullMark,
                    'labelId': labelId,
                    'standardNumber': label_eva_basis,
                },
                error: function () {
                    bootoast({
                        message: '通讯异常',
                        position: 'top',
                        type: 'danger',
                        timeout: 1.5,
                    });
                }
            });
        }
    }
}

function getTechnicalIndicatorData() {
    $.ajax({
        url: "/water_saving/get_technical_indicator_data",
        type: "get",
        async: false,
        data: 'standardNum=' + label_eva_basis,
        success: function (result) {
            list = result['rows'];
            insertTechnicalIndicatorData(list);
        },
        error: function () {
            bootoast({
                message: '通讯异常',
                position: 'top',
                type: 'danger',
                timeout: 1.5,
            });
        }
    });
}

function insertTechnicalIndicatorData(list) {
    //查询TECHNICAL_INDICATOR中的已有数据，插入时避免重复
    var assessmentContentList = [];
    $.ajax({
        url: "/table_water_saving/technical_select",
        data: 'last_id=' + label_id,
        type: "get",
        async: false,
        success: function (result) {
            var temp = result['rows'];
            for (var i = 0; i < temp.length; i++) {
                assessmentContentList.push(temp[i].assessmentContent);
            }
        },
        error: function () {
            bootoast({
                message: '通讯异常',
                position: 'top',
                type: 'danger',
                timeout: 1.5,
            });
        }
    });
    var assessmentContent;
    var technicalIndicator;
    var unit;
    var assessmentValue;
    var labelId;
    for (var i = 0; i < list.length; i++) {
        assessmentContent = list[i].evaluationContent;
        technicalIndicator = list[i].evaluationNumber;
        unit = list[i].company;
        assessmentValue = list[i].mark;
        labelId = label_id;
        var insertFlag = true;
        for (var j = 0; j < assessmentContentList.length; j++) {
            if (assessmentContent == assessmentContentList[j]) {
                insertFlag = false;
                break;
            }
        }
        if (insertFlag) {
            $.ajax({
                type: "post",
                url: "/water_saving/insert_technical_indicator_data",
                async: false,
                data: {
                    'assessmentContent': assessmentContent,
                    'technicalIndicator': technicalIndicator,
                    'unit': unit,
                    'assessmentValue': assessmentValue,
                    'labelId': labelId,
                    'standardNumber': label_eva_basis,
                },
                error: function () {
                    bootoast({
                        message: '通讯异常',
                        position: 'top',
                        type: 'danger',
                        timeout: 1.5,
                    });
                }
            });
        }
    }
}


